// ActionScript file
package org.liuhy.jbpm4
{
	import flash.events.FocusEvent;
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	import mx.containers.Canvas;
	import mx.containers.VBox;
	
	import org.liuhy.jbpm4.property.DecisionProperty;
	import org.liuhy.jbpm4.utils.JbpmRectLineUtils;
	import org.liuhy.line.AbstractLine;
	import org.liuhy.line.CLine;
	import org.liuhy.node.FlowNode;
	import org.liuhy.utils.MyStringUtils;
	
	public class DecisionFlowNode extends FlowNode
	{
		private var _language:String ;		//脚本语言;
		private var _handlerClass:String;	//类;
		private var _expr:String;			//表达式；
		private var _property:DecisionProperty = null;
		public function DecisionFlowNode(p_x:int,p_y:int,p_width:int,p_height:int)
		{
			super(p_x,p_y,p_width,p_height);	
			_property = new DecisionProperty(this);
//			this._textArea.addEventListener(FlexEvent.PREINITIALIZE,function(event:FlexEvent):void{
//				BindingUtils.bindProperty(_textArea,"text",_property._txtTitle,"text");
//			});
		}
		
		override public function draw():void{
		
			this.graphics.clear();
			this.graphics.beginFill(0xffffff,0);
			this.graphics.lineStyle(1,this.borderColor,2);
			this.graphics.moveTo(5,this.height/2);
			this.graphics.lineTo(this.width/2,5);
			this.graphics.lineTo(this.width-5,this.height/2);
			this.graphics.lineTo(this.width/2,this.height-5);
			this.graphics.endFill();
		}
		override public function onFocusChange(event:FocusEvent):void{
			super.onFocusChange(event);
			_property.title = this.title; 
		}

		override public function onClick(p_event:MouseEvent):void{
			super.onClick(p_event);
			var _vbox:VBox = this.parent.parent as VBox;
			var _c:Canvas = _vbox.getChildAt(1) as Canvas;
			var _cArray:Array = _c.getChildren();
			for(var i:int=0;i<_cArray.length;i++){
				(_cArray[i] as Canvas).visible = false;
			}
			if(!_c.contains(_property)){
				_c.addChild(_property);
			}else{
//				_property.expr = this._expr;
			}
			_property.visible = true;
			_c.setChildIndex(_property,_c.numChildren-1);			
		}
		override public function toXML():String{
			var _array:ArrayCollection = this._outLineList;
			var _str:String = "<" + this.type + " name=\"" + this.title + "\" ";
			if(""!=this.expr){
				_str = _str + " expr=\"" + MyStringUtils.getInstance().encodeXML(this.expr)+"\" ";
			}
			if(""!=this.language){
				_str = _str + " lang=\"" + MyStringUtils.getInstance().encodeXML(this.language) + "\" ";
			}
			_str = _str + " g=\"" + this.x +"," +this.y + "," + this.width + "," + this.height + "\">\n";

			if(""!=this.handlerClass){
				_str = _str + " <handler class=\"" + this.handlerClass + "\"/>";
			}
			for(var i:int=0;i<_array.length;i++){
				var _line:AbstractLine = _array[i] as AbstractLine;
				var _cLine:CLine = _line.parent as CLine;
				var _arrow:FlowNode = _cLine.getArrowFlowNode();
				var _to:String = "";
				var _tran:String = "";
				if(null!=_arrow){
					_to = _arrow.title;
				}

				if(0==JbpmRectLineUtils.getJbpmCLinePosStrs(_cLine).length){
					_tran= "<transition name=\"" + _cLine.title + "\" " + " to=\"" + _to + "\"/>\n";
				}else{
					_tran= "<transition name=\"" + _cLine.title + "\" " + " to=\"" + _to + "\" g=\"" + JbpmRectLineUtils.getJbpmCLinePosStrs(_cLine) + "\"/>\n";
				}
				_str = _str +  _tran ;
			}
			return _str + "</" + this.type + ">";
		}
		public function get expr():String{
			_expr = _property.expr;
			return _expr;
		}
		public function set expr(p_expr:String):void{
			this._expr 		 = p_expr;
			_property.expr	 = p_expr;
		}
		public function get language():String{
			_language = _property._txtLanguage.text;
			return _language;
		}
		public function set language(p_language:String):void{
			this._language 				 = p_language;
			_property._txtLanguage.text	 = p_language;
		}
		public function get handlerClass():String{
			_handlerClass = _property._txtHandlerClass.text;
			return _handlerClass;
		}
		public function set handlerClass(p_handlerClass:String):void{
			this._handlerClass 				 = p_handlerClass;
			_property._txtHandlerClass.text	 = p_handlerClass;
		}
		override public function get type():String{
			return "decision";
		}
	}
}